<template>
    <component :is="type" v-bind="linkProps">
        <slot></slot>
    </component>
</template>


<script setup lang="ts">
import { isExternal } from '@/utils/common'
interface Props {
    to: string | Record<string, string>
    replace?: boolean
}
const props = defineProps<Props>()

const isExternalLink = computed(() => {
    return typeof props.to !== 'object' && isExternal(props.to)
})


const type = computed(() => {
    if (isExternalLink.value) {
        return 'a'
    }
    return 'router-link'
})


const linkProps = computed(() => {
    if (isExternalLink.value) {
        return {
            href: props.to,
            target: '_blank'
        }
    }
    return props
})


</script>
